package rentcar.web;

import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import rentcar.domain.UserPrincipal;
import rentcar.service.UserService;

@Controller
@RequestMapping("/profile")
public class ProfileController {

	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
	public ModelAndView showUserProfile(
			@PathVariable String userId,
			@PathVariable("userId") UserPrincipal userModelTest,
			HttpSession session, Model model) {

		Locale locale = LocaleContextHolder.getLocale();
	    String language = locale.getLanguage();
	    
	    System.out.println("language="+language);
	    
		ModelAndView mav = new ModelAndView();
		mav.setViewName("profile");
		mav.addObject("userModelTest", userModelTest);
		
		Object userObject = session.getAttribute("user");
		if ((userObject != null) && (userObject instanceof UserPrincipal)) {
			UserPrincipal user = (UserPrincipal) userObject;
			
			System.out.println("Found an user with ID: " + user.getUserId());
		}
		
		return mav;
	}
}